package com.alipay.pluginapp;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.alipay.pluginframework.HookManager;
import com.alipay.pluginframework.ProxyActivityInterface;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

/**
 * @author: tanghaoshu
 * @date: 2021/9/23
 */
public class BaseActivity extends AppCompatActivity implements ProxyActivityInterface {
    //这里的that 指的是我们的宿主app，因为插件是没有安装的 是没有上下文的
    public AppCompatActivity that;
    

    @Override
    public void attach(AppCompatActivity proxyActivity) {
        that = proxyActivity;
    }

    @Override
    public void setContentView(View view) {//最终调用宿主的activity
        if (that != null) {
            that.setContentView(view);
        } else {
            super.setContentView(view);
        }
    }

    @Override
    public void setContentView(int layoutResID) {
        if (that == null){
            super.setContentView(layoutResID);
        }else {
            that.setContentView(layoutResID);
        }
    }

    public Activity getActivity(){
        return that == null?this:that;
    }

    @Override
    public View findViewById(int id) {
        if (that == null){
            return super.findViewById(id);
        }else {
            return that.findViewById(id);
        }

    }

    @Override
    public Intent getIntent() {
        if (that != null) {
            return that.getIntent();
        }
        return super.getIntent();
    }

    @Override
    public ClassLoader getClassLoader() {
        if (that != null) {
            return  HookManager.getInstance().getClassLoader();
        }else {
            return super.getClassLoader();
        }

    }

    @NonNull
    @Override
    public LayoutInflater getLayoutInflater() {
        if (that != null) {
            return that.getLayoutInflater();
        }else {
            return super.getLayoutInflater();
        }

    }

    @Override
    public ApplicationInfo getApplicationInfo() {
        if (that != null) {
            return that.getApplicationInfo();
        }else {
            return super.getApplicationInfo();
        }

    }

    @Override
    public Window getWindow() {
        if (that != null) {
            return that.getWindow();
        }else {
            return super.getWindow();
        }

    }


    @Override
    public WindowManager getWindowManager() {
        if (that != null) {
            return that.getWindowManager();
        }else {
            return super.getWindowManager();
        }
    }

    @Override
    public void startActivity(Intent intent) {
//        ProxyActivity --->className
        if (that == null){
           super.startActivity(intent);
        }else {
            Intent m = new Intent();
            m.putExtras(intent);
            m.putExtra("ClassName", intent.getComponent().getClassName());
            that.startActivity(m);
        }

    }

    public void startAppActivity(Intent intent) {
        if (that == null){

        }else {
            Intent m = new Intent();
            m.putExtras(intent);
            m.putExtra("isPlugin",false);
            that.startActivity(m);
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (that == null) {
            super.onCreate(savedInstanceState);
        }
    }

    @Override
    public void onStart() {
        if (that == null) {
            super.onStart();
        }
    }

    @Override
    public void onResume() {
        if (that == null) {
            super.onResume();
        }
    }

    @Override
    public void onPause() {
        if (that == null) {
            super.onPause();
        }
    }

    @Override
    public void onStop() {
        if (that == null) {
            super.onStop();
        }
    }

    @Override
    public void onDestroy() {
        if (that == null) {
            super.onDestroy();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        if (that == null) {
            super.onSaveInstanceState(outState);
        }
    }

    @Override
    public Resources getResources() {
        if (that == null) {
            return super.getResources();
        }else {
            return  HookManager.getInstance().getResource();
        }

    }
}
